extends CharacterBody2D

signal place_object_at_input_button

var result_color: String
## Asaka speed modifier, increase to increase asaka speed
var speed_modifier: int = 100 ## Add to menu settings
## Preload 4 asaka textures
var asaka_top = preload("res://textures/asaka/asaka_top.png") ## Top
# var asaka_top_left = preload("res://textures/asaka/asaka_top_left.png") ## Top-left
var asaka_left = preload("res://textures/asaka/asaka_left.png") ## Left
var asaka_bottom = preload("res://textures/asaka/asaka_bottom.png") ## Bottom
var asaka_right = preload("res://textures/asaka/asaka_right.png")

var current_ui
## object of output button asaka stays
var current_button_output_object = "void"
## object of input button asaka stays
#### var current_button_input_object = "void"

## When asaka pickup items, items placed in this array 
var wand_invertory: Array = ["void", "void"]

## Output slots in game, items in this array can placed at input buttons
#var exit_inventory: Array = ["void"]

var slot_select: int = 0 ## selected slot in inventory

# Called when the node enters the scene tree for the first time.
func _ready():
	GlobalVar.in_game = true
	load_wand_ui(GlobalVar.current_level)

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	## Asaka movement
	velocity = speed_modifier * Input.get_vector("left", "right", "up", "down")
	move_and_slide()
	
	## Texture change based on movement direction
	if Input.is_action_just_pressed("down"):
		$Sprite2D.texture = asaka_top
	elif Input.is_action_just_pressed("left"):
		$Sprite2D.texture = asaka_right
	elif Input.is_action_just_pressed("right"):
		$Sprite2D.texture = asaka_left
	elif Input.is_action_just_pressed("up"):
		$Sprite2D.texture = asaka_bottom
		
	## asaka pickup in wand inventory
	if Input.is_action_just_pressed("pickup") and not GlobalVar.mouse_input_lock:
		wand_invertory[slot_select] = current_button_output_object
		current_ui.ui_object_color(slot_select, current_button_output_object)
		print("wand_inventory: ", wand_invertory)

	## asaka inventory scroll
	if Input.is_action_just_pressed("inventory_scroll_right") and not GlobalVar.mouse_input_lock:
		if slot_select == wand_invertory.size() - 1:
			slot_select = 0
		else:
			slot_select += 1	 
		print("slot: ", slot_select)
	if Input.is_action_just_pressed("inventory_scroll_left") and not GlobalVar.mouse_input_lock: 
		if slot_select == 0:
			slot_select = wand_invertory.size() - 1
		else:
			slot_select += -1	 
		print("slot: ", slot_select)
		
	## asaka * objects
	if Input.is_action_just_pressed("operation") and not GlobalVar.mouse_input_lock:
		if GlobalVar.current_level == 1:
			GlobalVar.exit_inventory[GlobalVar.exit_slot] = wand_invertory[slot_select]
			current_ui.ui_object_color(1, wand_invertory[slot_select])
			print(GlobalVar.exit_inventory)     
		elif GlobalVar.current_level == 2:
			if wand_invertory[0] == "red" and wand_invertory[1] == "blue":
				GlobalVar.exit_inventory[GlobalVar.exit_slot] = "red"
				current_ui.ui_object_color(2, "red")
			elif wand_invertory[0] == "blue" and wand_invertory[1] == "red":
				GlobalVar.exit_inventory[GlobalVar.exit_slot] = "blue"
				current_ui.ui_object_color(2, "blue")
			else:
				push_error("Invalid operation")
		elif GlobalVar.current_level == 3:
			result_color = level_3_operation(wand_invertory[0], wand_invertory[1])
			GlobalVar.exit_inventory[GlobalVar.exit_slot] = result_color
			current_ui.ui_object_color(2, result_color)

## Load level UI
func load_wand_ui(level):
	if level == 1:
		$UI/ui_2.visible = false
		$UI/ui.visible = true
		current_ui = $UI/ui
		wand_invertory.resize(1)
	else:
		$UI/ui.visible = false
		$UI/ui_2.visible = true
		current_ui = $UI/ui_2
		wand_invertory.resize(2)

func level_3_operation(slot_1, slot_2):
	if slot_1 == "red" and slot_2 == "red":
				return "red"
	elif slot_1 == "red" and slot_2 == "blue":
				return "blue"
	elif slot_1 == "blue" and slot_2 == "red":
				return "blue"
	elif slot_1 == "red" and slot_2 == "yellow":
				return "yellow"
	elif slot_1 == "yellow" and slot_2 == "red":
				return "yellow"
	elif slot_1 == "blue" and slot_2 == "yellow":
				return "red"
	elif slot_1 == "yellow" and slot_2 == "blue":
				return "red"
	elif slot_1 == "blue" and slot_2 == "blue":
				return "yellow"
	elif slot_1 == "yellow" and slot_2 == "yellow":
				return "blue"
	else:
				return "void"
		
